import inspect
import re

from params import AParams,BaseParam

func_params = AParams()

int_xml_tag = re.compile(r'<int>(.*?)</int>')
str_xml_tag = re.compile(r'<str>(.*?)</str>')
bool_xml_tag = re.compile(r'<bool>(.*?)</bool>')
list_xml_tag = re.compile(r'<list>(.*?)</list>')

res_type_dict = {
    'int': int_xml_tag,
    'str': str_xml_tag,
    'bool':bool_xml_tag,
    'list':list_xml_tag
}

# 函数的输入参数和类型导入类中
def match_input_type(s:str):
    global func_params
    s = s.replace(' ','').split('->')[0][1:-1]   # 默认消除所有的空格
    # print(s)
    for i in s.split(','):
        func_params.input_append(*i.split(':'))
    print(func_params.params_input[0], func_params.params_input[1])

# 函数的输出参数和类型导入类中
def match_return_type(s:str, func_doc:str)->tuple[str]:
    global func_params
    j = re.compile(r'\[(.*?)\]')
    s_new = j.findall(s)[0].replace(' ','') # 清除空格，输出类型
    for param_type in s_new.split(','):
        param_name = res_type_dict[param_type].findall(func_doc)[0]
        # print(param_name)
        func_params.output_append(param_name,param_type)
    print(func_params.params_output[0], func_params.params_output[1])
    return '',

# 规则：用中括号将返回值类型框起来
def ajy_main(num1:int, num2:int)->tuple[int,str]:
    """
    注释用来声明返回值类型的名称——便于变量的传递
    :param num1:
    :param num2:
    :return:
    <return>
        <int>res_int</int>
        <str>res_str</str>
    </return>
    """
    res = num1+num2
    return res,''



if __name__ == '__main__':
    signature2 = inspect.signature(ajy_main)
    print(signature2)    # (num1: int, num2: int) -> tuple[int, str]
    signature2 = str(signature2)
    match_input_type(signature2)
    # 输出   函数的参数列表：OrderedDict([('num1', < Parameter "num1: int" >)
    params = inspect.signature(ajy_main)
    # print(params, type(params), str(params), type(str(params)))

    # 有注释则输出注释的类型，没有注释则输出inspect._empty
    return_type = inspect.signature(ajy_main).return_annotation
    # print(return_type, type(return_type), str(return_type), type(str(return_type)))

    return_type = str(return_type)
    match_return_type(return_type,ajy_main.__doc__)

    # print(ajy_main.__doc__)



